package com.villaday.idf.custom.DividerItemDecoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.view.View;


/**
 * Created by Haozi on 2018/10/10 0023.
 */

public class RVGridDividerItemDecoration extends RecyclerView.ItemDecoration {
    private Paint mPaint;
    private int mDividerWidth = 0;          //您所需指定的间隔宽度，主要为第一列和最后一列与父控件的间隔；行间距，列间距将动态分配
    int spanCount = 0;
    private Context mContext;
    private int color = Color.RED;
    private int headerCount = 0;

    /**
     * @param dividerWidth 间隔宽度
     */
    public RVGridDividerItemDecoration(Context context, int dividerWidth) {
        mContext = context;
        mDividerWidth = dividerWidth;

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * @param dividerWidth 间隔宽度
     * @param color        间隔颜色
     */
    public RVGridDividerItemDecoration(Context context, int dividerWidth, @ColorInt int color) {
        mContext = context;
        mDividerWidth = dividerWidth;
        this.color = color;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);

    }

    /**
     * @param context
     * @param dividerWidth 间隔宽度
     * @param color        间隔颜色
     * @param headerCount  头部数量
     */
    public RVGridDividerItemDecoration(Context context, int dividerWidth, @ColorInt int color, int headerCount) {
        this(context, dividerWidth, color);
        this.headerCount = headerCount;
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();

         spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();
        if (position >= headerCount) {
            if (isFirstColumn(parent, position, spanCount, childCount)){//第一列
                outRect.set(mDividerWidth, mDividerWidth, mDividerWidth/2, 0);

            } else if (isLastRaw(parent, position, spanCount, childCount)) {// 如果是最后一行，则不需要绘制底部
                outRect.set(mDividerWidth, 0, mDividerWidth, 0);

            } else if (isLastColumn(parent, position, spanCount, childCount)) {// 如果是最后一列，则不需要绘制右边
                outRect.set(mDividerWidth/2, mDividerWidth, mDividerWidth, 0);
            } else {
                outRect.set(mDividerWidth/2, mDividerWidth, 0, 0);
            }

        }


    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        draw(c, parent);
    }

    //绘制item分割线
    private void draw(Canvas canvas, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();

            //画水平分隔线
            int left = child.getLeft();
            int right = child.getRight();
            int top = child.getBottom() + layoutParams.bottomMargin;
            int bottom = top + mDividerWidth;
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }

            //画垂直分割线
            if (!isLastColumn(parent, i, spanCount, childCount)){


                top = child.getTop();
                bottom = child.getBottom() + mDividerWidth;
                left = child.getRight() + layoutParams.rightMargin;
                right = left + mDividerWidth;
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }

        }
    }

    /**
     * 判读是否是第一列
     *
     * @param parent
     * @param position
     * @param spanCount
     * @param childCount
     * @return
     */
    private boolean isLastColumn(RecyclerView parent, int position, int spanCount, int childCount) {
        if (position < headerCount) {
            return true;
        }
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((position - headerCount +1) % spanCount == 0) {// 如果是最后一列，则不需要绘制右边
                return true;
            } else {
                return false;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((position - headerCount+1 ) % spanCount == 0) {// 如果是最后一列，则不需要绘制右边
                    return true;
                } else {
                    return false;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if (position >= childCount) {// 如果是最后一列，则不需要绘制右边
                    return true;
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 第一列
     */
    private boolean isFirstColumn(RecyclerView parent, int position, int spanCount, int childCount) {

        if (position < headerCount) {
            return true;
        }
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((position - headerCount ) % spanCount == 0) {// 如果是第一列，则不需要绘制左边
                return true;
            } else {
                return false;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((position - headerCount ) % spanCount == 0) {// 如果是第一列，则不需要绘制左边
                    return true;
                } else {
                    return false;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if (position >= childCount) {// 如果是第一列，则不需要绘制左边
                    return true;
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 判断是否是最后一列
     *
     * @param parent
     * @param position
     * @param spanCount
     * @param childCount
     * @return
     */
    private boolean isLastRaw(RecyclerView parent, int position, int spanCount, int childCount) {

        if (position < headerCount) {
            return true;
        }
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            childCount = childCount - childCount % spanCount;
            if (position >= childCount)// 如果是最后一行，则不需要绘制底部
                return false;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            // StaggeredGridLayoutManager 且纵向滚动
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行，则不需要绘制底部
                if (position >= childCount) {
                    return true;
                }
            } else {// StaggeredGridLayoutManager 且横向滚动
                // 如果是最后一行，则不需要绘制底部
                if ((position + 1) % spanCount == 0) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 获取列数
     *
     * @param parent
     * @return
     */
    private int getSpanCount(RecyclerView parent) {
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return spanCount;
    }
}

